/*
 * 测试GPS程序
 *   接线：
 *                  GPS （+） ----- （+） 外接5V电源
 *   MKR GND------- GPS （-）------ （-） 外接5V电源
 *                  GPS （TXD）-------> MKR  D1
 *        
 */ 


#include <Arduino.h>                              // required before wiring_private.h
#include <wiring_private.h>
#include <TinyGPS++.h>
static const char MKR1000_LED       = 6;

// Serial2 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
#define PIN_SERIAL2_RX       (1ul)                // Pin description number for PIO_SERCOM on D1
#define PIN_SERIAL2_TX       (0ul)                // Pin description number for PIO_SERCOM on D0
#define PAD_SERIAL2_TX       (UART_TX_PAD_0)      // SERCOM pad 0 TX
#define PAD_SERIAL2_RX       (SERCOM_RX_PAD_1)    // SERCOM pad 1 RX

// Serial3 pin and pad definitions (in Arduino files Variant.h & Variant.cpp)
#define PIN_SERIAL3_RX       (5ul)                // Pin description number for PIO_SERCOM on D5
#define PIN_SERIAL3_TX       (4ul)                // Pin description number for PIO_SERCOM on D4
#define PAD_SERIAL3_TX       (UART_TX_PAD_2)      // SERCOM pad 2 TX
#define PAD_SERIAL3_RX       (SERCOM_RX_PAD_3)    // SERCOM pad 3 RX

// Instantiate the extra Serial classes
Uart Serial2(&sercom3, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX);
Uart Serial3(&sercom4, PIN_SERIAL3_RX, PIN_SERIAL3_TX, PAD_SERIAL3_RX, PAD_SERIAL3_TX);
TinyGPSPlus gps; 
void SERCOM3_Handler()    // Interrupt handler for SERCOM3
{
  Serial2.IrqHandler();
}

void SERCOM4_Handler()    // Interrupt handler for SERCOM4
{
  Serial3.IrqHandler();
}

void setup()
{
  static boolean state = HIGH;
  static unsigned char count = 0;
  
  pinMode(MKR1000_LED, OUTPUT);
  Serial1.begin(9600);
  
  // Start Serial for debugging on the Serial Monitor
  Serial.begin(9600);
  while (!Serial && (count < 30) )
  {
    delay(200);     // Wait for serial port to connect with timeout. Needed for native USB
    digitalWrite(MKR1000_LED, state);
    state = !state;
    count++;
  }

  pinPeripheral(0, PIO_SERCOM);   // Assign pins 0 & 1 SERCOM functionality
  pinPeripheral(1, PIO_SERCOM);
  Serial2.begin(9600);           // Begin Serial2 

  pinPeripheral(4, PIO_SERCOM_ALT);   // Assign pins 4 & 5 SERCOM functionality
  pinPeripheral(5, PIO_SERCOM_ALT);
  Serial3.begin(9600);               // Begin Serial3

  digitalWrite(MKR1000_LED, HIGH);

  Serial.println("Setup Complete");
  Serial1.println("Setup Complete");
  Serial2.println("Setup Complete");
  Serial3.println("Setup Complete");
}

void loop()
{
  static unsigned long ms = 0;
  static boolean state = HIGH;
  static unsigned char c = 0;

//L--  if (Serial.available())
//L--  {
//L--    char c = Serial.read();
//L--    Serial.print(c);
//L--    Serial1.print(c);
//L--    Serial2.print(c);
//L--    Serial3.print(c);
 //L-- }
 //L-- if (Serial1.available())
 //L-- {
 //L--   char c = Serial1.read();
 //L--   Serial.print(c);
 //L--   Serial1.print(c);
 //L--   Serial2.print(c);
 //L--   Serial3.print(c);
 //L-- }
 //L--  if (Serial2.available())
 //L--  {
 //L--    char c = Serial2.read();
 //L--    Serial.print(c);
 //L--   Serial1.print(c);
 //L--   Serial2.print(c);
 //L--   Serial3.print(c);
 //L--  }
 //L-- if (Serial3.available())
 //L-- {
 //L--   char c = Serial3.read();
 //L--   Serial.print(c);
 //L--   Serial1.print(c);
 //L--   Serial2.print(c);
 //L--   Serial3.print(c);
 //L-- }


 //L--  if (millis() - ms > 100)
 //L--  {
 //L--    ms = millis();
 //L--    digitalWrite(MKR1000_LED, state);
 //L--    state = !state;
 //L--  }


 if (Serial1.available() > 0) {
    if (gps.encode(Serial1.read())) {
      if (gps.location.isValid()) {
        Serial.print(F("- latitude: "));
        Serial.println(gps.location.lat());

        Serial.print(F("- longitude: "));
        Serial.println(gps.location.lng());

        Serial.print(F("- altitude: "));
        if (gps.altitude.isValid())
          Serial.println(gps.altitude.meters());
        else
          Serial.println(F("INVALID"));
      } else {
        Serial.println(F("- location: INVALID"));
      }
/*
      Serial3.print(F("- speed: "));
      if (gps.speed.isValid()) {
        Serial3.print(gps.speed.kmph());
        Serial3.println(F(" km/h"));
      } else {
        Serial3.println(F("INVALID"));
      }

      Serial3.print(F("- GPS date&time: "));
      if (gps.date.isValid() && gps.time.isValid()) {
        Serial3.print(gps.date.year());
        Serial3.print(F("-"));
        Serial3.print(gps.date.month());
        Serial3.print(F("-"));
        Serial3.print(gps.date.day());
        Serial3.print(F(" "));
        Serial3.print(gps.time.hour()+8);
        Serial3.print(F(":"));
        Serial3.print(gps.time.minute());
        Serial3.print(F(":"));
        Serial3.println(gps.time.second());
      } else {
        Serial3.println(F("INVALID"));
      }*/

      Serial.println();
    }
  

  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));
 }
 }
